package com.hudilao.controller;


import com.hudilao.common.BaseContext;
import com.hudilao.common.R;
import com.hudilao.dto.DishSaleDTO;
import com.hudilao.dto.OrderDetailDTO;
import com.hudilao.entity.OrderDetail;
import com.hudilao.service.OrderDetailService;
import com.hudilao.service.OrderService;
import com.hudilao.vo.OrderHistoryDetailVO;
import com.hudilao.vo.OrderHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user/orderDetail")
public class OrderDetailController {
    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private OrderService orderService;

    /**
     * 用户查询历史订单详情
     * @param orderId
     * @return
     */
    @GetMapping("/list/{orderId}")
    public R<OrderHistoryVO> list(@PathVariable Long orderId){

        //获取当前用户
        long customerId = BaseContext.getCurrentId();

        OrderHistoryVO orderHistory = orderService.getOrderById(orderId);
        if (orderHistory != null) {
            return R.success(orderHistory);
        } else {
            return R.error("没有找到历史订单");
        }
    }


    //管理端：统计报表
    @GetMapping("/sell")
    public R<List<DishSaleDTO>> getTopSellingDishes(@RequestParam(value = "timeRange", required = false, defaultValue = "daily") String timeRange) {
        //获取销量前五菜品名称和数量
        List<DishSaleDTO> topSellingDishes = orderDetailService.getTopSellingDishes(timeRange);

        return R.success(topSellingDishes);
    }
}
